//
//  AppViewModel.m
//  HGMMRAC
//
//  Created by eport on 2021/1/19.
//

#import "AppViewModel.h"
#import "MamServer.h"
#import "AppViewModel.h"
#import <YYModel/YYModel.h>
#import "AuthServer.h"

@implementation AppViewModel

-(instancetype)init{
    if (self = [super init]) {
        [self bindingSomething];
       
    }
    return  self;
}





-(void)bindingSomething{
    
    @weakify(self);
    self.loadCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input){
       
        @strongify(self);
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            dispatch_async(dispatch_get_global_queue(0, 0), ^{

                [MamServer getPluginListWithPage:1 Success:^(NSDictionary * _Nonnull resultDic) {

                    dispatch_async(dispatch_get_main_queue(), ^{


                        NSArray *array = resultDic[@"plugins"];


                        NSArray *modelsArray = [array.rac_sequence map:^id _Nullable(NSDictionary   * _Nullable value) {
                            AppModel *model = [AppModel yy_modelWithDictionary:value];
                            return model;
                        }].array;

                        if (modelsArray.count > 0) {
                            self.dataArray = modelsArray;
                        }
                        NSLog(@"_______%@",[NSThread currentThread]);

                        [subscriber sendNext:nil];
                        [subscriber sendCompleted];

                    });


                } failure:^(NSError * _Nonnull error) {

                    dispatch_async(dispatch_get_main_queue(), ^{
                        [subscriber sendError:error];
                    });

                }];


            });
            
        
            return nil;
        }];
        
    }];
    
 
    
}



@end
